import Point2D from "../../standard/entities/Point/Point2D";
import BaseLifeCyclePainting from "../../standard/entities/Painting/BaseLifeCyclePainting";
import Circular from "../../standard/entities/Shape/Circular";

export default class BubblePainting extends BaseLifeCyclePainting {

    constructor() {
        super();
        this.speed = parseInt(Math.random() + 1.5);
        this.r = parseInt(Math.random() * +3);
        this.count = {
            init: 0,
            running: 0,
            destroyed: 0
        };
    }

    beDrawn(painter) {
        painter.drawInSession(() => {
            painter.beginPath();
            painter.arc(this.circle.centre.x, this.circle.centre.y, this.circle.r, 0, 2 * Math.PI);
            painter.fill();
        });
    }

    onPainterLoaded(painter) {
        super.onPainterLoaded(painter);
        painter.globalAlpha = 0.5;
        painter.fillStyle = '#fff';
    }

    onCanvasLoaded(canvas) {
        super.onCanvasLoaded(canvas);
        this.point = new Point2D(parseInt(Math.random() * this.canvas.width), this.canvas.height);
        this.circle = new Circular(this.r, this.point);
    }

    nextFrameOnInit(controller) {
        super.nextFrameOnInit(controller);
        this.point.y -= this.speed - 1;
        this.circle.r += 0.1;
        if (this.count.init++ >= 100) {
            controller.setStatus(BaseLifeCyclePainting.RUNNING);
        }
    }

    nextFrameOnRunning(controller) {
        super.nextFrameOnInit(controller);
        this.circle.centre.y -= this.speed;
        this.r += 0.02;
        if (this.count.running++ >= 500) {
            controller.setStatus(BaseLifeCyclePainting.DEATH);
        }
    }

    nextFrameOnDeath(controller) {
        super.nextFrameOnInit(controller);
        this.circle.r += 0.5;
        this.painter.globalAlpha -= 0.01;
        if (this.painter.globalAlpha <= 0) {
            this.painter.globalAlpha = 0;
        }
        if (this.count.destroyed++ >= 50) {
            controller.setStatus(BaseLifeCyclePainting.DESTROYED);
        }
    }
}